FROM node:22.0.0 AS frontend-builder
LABEL maintainer="mwh"
WORKDIR /app
RUN git clone https://github.com/open-webui/open-webui.git
WORKDIR /app/open-webui
RUN npm install && \
    npm run build

FROM python:3.11-slim AS backend-builder

RUN apt-get update && \
    apt-get install -y curl && \
    curl -fsSL https://ollama.com/install.sh | sh

COPY --from=frontend-builder /app/open-webui /app/open-webui

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    curl gcc python3-dev libsm6 libxext6 && \
    rm -rf /var/lib/apt/lists/*


WORKDIR /app/open-webui/backend
RUN pip install --no-cache-dir -r requirements.txt


RUN echo '#!/bin/bash\n\
ollama serve &\n\
sleep 5\n\
ollama pull deepseek-r1:1.5b\n\
ollama run deepseek-r1:1.5b\n\
cd /app/open-webui/backend\n\
exec bash start.sh' > /app/run.sh && \
    chmod +x /app/run.sh

CMD ["/app/run.sh"]